package com.bosch.in.rtp1.ept.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bosch.in.rtp1.ept.constant.ReleasedHoldParameter;
import com.bosch.in.rtp1.ept.dao.RelHldDiagramDao;
import com.bosch.in.rtp1.ept.model.dto.RelHldChartFactor;
import com.bosch.in.rtp1.ept.model.technical.Condition4RelHldDiagram;
import com.bosch.in.rtp1.ept.util.SQLStatementLoader;

public class RelHldDiagramJDBCDao extends GenericDao implements RelHldDiagramDao{

	/**
	 * Constructor to inject connection
	 * @param con	DB connection
	 */
	public RelHldDiagramJDBCDao(Connection connection) {
		super(connection);
	}

	@Override
	public List<RelHldChartFactor> getFactorList(
			Condition4RelHldDiagram condition4RelHldDiagram)
			throws SQLException {
		
		//load query statement
		String queryString = SQLStatementLoader.getRelHldFactorsQuery();
		
		//execute query
		ResultSet resultSet = executeQuery(queryString,condition4RelHldDiagram.getParameterValues().toArray());
		
		//examine result set
		List<RelHldChartFactor> relHldChartFactors = new ArrayList<RelHldChartFactor>();
		while (resultSet.next()) {
			RelHldChartFactor rFactor= new RelHldChartFactor();
			rFactor.setMonth(resultSet.getInt(ReleasedHoldParameter.MONTH_DB));
			rFactor.setYear(resultSet.getInt(ReleasedHoldParameter.YEAR_DB));
			rFactor.setPrio1(getFloatObjFromResultSet(ReleasedHoldParameter.CHART_PRIO1_DB,resultSet));
			rFactor.setPrio2(getFloatObjFromResultSet(ReleasedHoldParameter.CHART_PRIO2_DB,resultSet));
			rFactor.setPrio3(getFloatObjFromResultSet(ReleasedHoldParameter.CHART_PRIO3_DB,resultSet));
			rFactor.setCountPrio1(resultSet.getInt(ReleasedHoldParameter.COUNT_PRIO_1_DB));
			rFactor.setCountPrio2(resultSet.getInt(ReleasedHoldParameter.COUNT_PRIO_2_DB));
			rFactor.setCountPrio3(resultSet.getInt(ReleasedHoldParameter.COUNT_PRIO_3_DB));
			relHldChartFactors.add(rFactor);
		}
		
		// close statement, result set
		resultSet.close();
		close();		
		return relHldChartFactors;
	}
	
	/***********PRIVATE METHOD****************/
	
	/**
	 * Get float value of a field in resultSet
	 * @param column: index of the field in resultSet
	 * @param resultSet : ResultSet object 
	 * @return  + NULL if value of inputed field is NULL
	 * 			+ float value of that field in other cases  
	 *     
	 */
	private Float getFloatObjFromResultSet(String column,ResultSet resultSet) throws SQLException
	{
		if (resultSet.getString(column) == null)
		{
			return null;
		}
		else
		{
			return resultSet.getFloat(column);
		}
	}

}
